package controllers.manage.product;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import product.Company;
import util.common.DateUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/12.
 */
@With(BaseController.class)
public class CompanyCtrl extends Controller{

    //查询
    public static void index(Integer pageNumber, String name){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();
        if(StringUtils.isNotBlank(name)){
            searchMap.put("name", "%"+name+"%");
        }
        JPAExtPaginator<Company> resultPage = Company.findByCondition(searchMap,"createAt desc", pageNumber, GlobalConfig.PAGE_SIZE);
        for(Company company:resultPage){
            company.createAtStr= DateUtil.dateToString(company.createAt,"yyyy-MM-dd");
        }
        render(resultPage, pageNumber,name);
    }
    public static void add(){
        render();
    }
    //新增
    public static void create(Company company){
        AdminUser adminUser = Secure.getLoginUser();
        company.deleted= DeletedStatus.UN_DELETED;
        company.createAt=new Date();
        company.creator=adminUser;
        company.save();
        flash.put("msg" , "公司捐赠信息添加成功");
        redirect("/BS0009/admin/product/CompanyCtrl/index/1");
    }
    public static void modify(Long id,Integer pageNumber){
        Company company=Company.findById(id);
        render(company,pageNumber);
    }
    public static void update(Long id,Integer pageNumber,Company company){
            AdminUser adminUser = Secure.getLoginUser();
            company.modifyAt=new Date();
            company.modifier=adminUser;
            Company.update(id,company);
            flash.put("msg" , "公司捐赠信息修改成功");
            redirect("/BS0009/admin/product/CompanyCtrl/index/"+pageNumber);
    }
    public static void delete(Long id,Integer pageNumber){
        AdminUser adminUser = Secure.getLoginUser();
        Company company=Company.findById(id);
        company.deleted=DeletedStatus.DELETED;
        company.modifyAt=new Date();
        company.modifier=adminUser;
        company.save();
        flash.put("msg" , "删除成功");
        redirect("/BS0009/admin/product/CompanyCtrl/index/"+pageNumber);
    }
}
